let db = wx.cloud.database({
  env: 'mini-cloud-8gonte8a94b9dcfb'
});
let poemsl = db.collection('db_poemsl');
let author = db.collection('db_authors');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr:[],
    post:[
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/libai.jpg",
        text:"李白"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/xinqiji.png",
        text:"辛弃疾"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/luyou.png",
        text:"陆游"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/dufu.png",
        text:"杜甫"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/baijuyi.png",
        text:"白居易"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/wanganshi.png",
        text:"王安石"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/suhsi.jpg",
        text:"苏轼"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/liqinghzao.jpg",
        text:"李清照"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/wangwei.png",
        text:"王维"
      },
      {
        img:"cloud://mini-cloud-8gonte8a94b9dcfb.6d69-mini-cloud-8gonte8a94b9dcfb-1317695117/zhangyanghao.png",
        text:"张养浩"
      },
    ],
    comArr:[],
    num:3,
    state:false
  },
  top(){
    wx.pageScrollTo({
      scrollTop: 0,
      
     })
  },
  onPageScroll(event) {

      // console.log(event.scrollTop);
      if(event.scrollTop>667){
        this.setData({
          state:true
        })
      }else{
        this.setData({
          state:false
        })
      }
  },
  tiao(event){
    let id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: `../detail/detail?id=${id}`,
    })
  },
  jump(event){
    let name=event.currentTarget.dataset.name
    wx.navigateTo({
      url: `../authordetail/authordetail?name=${name}`,
    })
  },  
  detail(event){
    let name=event.currentTarget.dataset.name
    wx.navigateTo({
      url: `../authordetail/authordetail?name=${name}`,
    })
  },
  search(){
    wx.switchTab({
      url: `../search/search`,
    })
  },
  switch(){
    let num=Math.floor(Math.random() *10000+1)
       wx.showLoading({
            title: "正在加载...",
            icon: "none",
          });
    poemsl.where({}).skip(num).limit(20)
    .get()
    .then(res => {
       this.setData({
          arr:res.data
        })
         wx.hideLoading();
    })
  },
  onLoad(options) {
    let numArr=Math.floor(Math.random() *10+1)
       wx.showLoading({
            title: "正在加载...",
            icon: "none",
          });
    poemsl.where({}).skip(numArr).limit(1)
    .get()
    .then(res => {
       this.setData({
        comArr:res.data[0]
        })
         wx.hideLoading();
    })

    if(this.data.arr.length!=0){
      return
    }
     wx.showLoading({
            title: "正在加载...",
            icon: "none",
          });
    poemsl.where({}).get().then(content=>{
      let{data,errMsg} =content
      data.splice(7,1)
      data.splice(9,2)
      data.splice(12,1)
      // console.log(data);
      this.setData({
      arr:data
    })
    wx.hideLoading();
    }).catch(err=>{
      console.log(err);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.num++
    poemsl.where({}).skip(this.data.num).limit(20)
    .get()
    .then(res => {
      let newArr=res.data
      let arr=this.data.arr.concat(newArr)
      this.setData({
        arr
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})